#ifndef TCV_DMHANDLE_H
#define TCV_DMHANDLE_H

#include "tcv_config.h"
#include "tcv_decthandle.h"
#include "tcv_measure.h"
#include <QImage>
#include <QObject>
#include <QWidget>


#include <vector>
#include <queue>
#include <mutex>
#include <thread>

class TCV_DectHandle;
QImage MatToQImage(const cv::Mat& mat);

class TCV_DMHandle:public QObject
{
    Q_OBJECT
public:
    TCV_DMHandle();
    bool SearchCameraID(std::vector<unsigned int> &o_cameraID);
    bool OpenTwoCamera(unsigned int i_cameraId1, unsigned int i_cameraId2, int &o_errorNum);
    bool OpenOneCamera(unsigned int i_cameraId, int &o_errorNum);
    bool CloseCarmera();

    bool IsCameraOpen(unsigned int i_whichOneInt);
    bool CatchPicture(unsigned char i_whichOneU8);
    bool CatchPicture(unsigned char i_whichOneU8, int &o_errorNum);
    bool CopyPicture(unsigned int i_whichOneInt, cv::Mat &o_pictureMat);
    bool CopyPicture(unsigned int i_whichOneInt, QImage &o_pictureQImage);
    bool ClonePictureToInputQueue(unsigned char i_whichOneU8);
    bool PopPictureFromInputQueue(unsigned int i_whichOneInt, cv::Mat &o_matPicture);
    bool CloneDectResToOutputQueue(cv::Mat i_matPicture);
    bool PopDectResFromOutputQueue(cv::Mat &o_matPicture);
    bool ClearPictureInQueue();
    int GetQueuePlSize();

    bool tcv_dectect(cv::Vec3f &o_distance, cv::Point &o_locate, int &o_errorNum);
    bool tcv_measure(cv::Mat picture_left, cv::Mat picture_right, cv::Vec3f &o_distance, cv::Point &o_locate, int &o_errorNum);

signals:
    void SN_DectFinishDMDect(cv::Point point);

private slots:
    void SL_DectFinishRecall(int signal_num);

private:
    //config
    const int m_imageWidth;
    const int m_imageHeight;
    const unsigned int mc_matQueueSizeLimit=8;

    //Pl:prime left;    Vr:vice right
    unsigned int m_cameraIdPl,m_cameraIdVr;
    cv::VideoCapture m_VideoCapture_Pl,m_VideoCapture_Vr;
    cv::Mat m_Mat_PicturePl,m_Mat_PictureVr;

    //multi thread
    mutable std::timed_mutex m_TimedMut_MatQueuePl;
#define _TimedmutTimeout 100
    std::queue<cv::Mat> m_Queue_MatPl,m_Queue_MatVr,m_Queue_MatDectOutput;
    TCV_DectHandle *m_DectHandle;
    TCV_MeasureHandle *m_MeasureHandle;

};

#endif // CV_DMHANDLE_H
